import systemFunctionService from "../../service/systemFunctionService"
import { Request, Response } from 'express'

class SystemFunctionController {
  // 获取全部
  async findAll(req: Request, res: Response) {
    const result = await systemFunctionService.findAll()
    res.gsmd(result)
  }

  // 更新状态
  async updateState(req: Request, res: Response) {
    const { id } = req.params
    const findRes = await systemFunctionService.findById(id)
    const updateRes = await systemFunctionService.updateState(id, !findRes.state)
    if (updateRes.affected < 1) {
      return res.gem('修改失败！')
    } else {
      return res.gsm('修改成功！')
    }
  }

  // 根据角色、类型查询对应功能
  async findByRoleAndType(req: Request, res: Response) {
    const { role, type } = req.params
    const findRes = await systemFunctionService.findByRoleAndType(Number(role), type)
    if (findRes) {
      res.gsmd(findRes)
    } else {
      res.gem('查询失败，请检查参数！')
    }
  }

}

export default new SystemFunctionController()